package cn.minimal;

import java.util.Random;

public class eTank implements Runnable {
    int x = 0;
    int y = 0;
    int type = 0; // 0 - 我方坦克，1 - 敌方坦克
    int direction = 0; // 0 - 方向向上，1 - 方向向右，2 - 方向向下，3 - 方向向左
    int speed = 3;
    Shot shot = null;
    Bomb bomb = null;
    int times = 100;
    int r = 2;
    int bomb_time = 3;
    int revive_time = 5;
    boolean isLive = true;
    Random generator = new Random();
    public int getSpeed() {
            return speed;
        }
    public void setSpeed(int speed) {
            this.speed = speed;
        }

    public eTank(int x, int y, int type, int direction) {
        this.x = x;
        this.y = y;
        this.type = type;
        this.direction = direction;
    }

    public int getX() {
            return x;
        }
    public void setX(int x) {
            this.x = x;
        }

    public int getY() {
            return y;
        }

    public void setY(int y) {
            this.y = y;
        }

    public int getType() {
            return type;
        }

    public void setType(int type) {
            this.type = type;
        }

    public int getDirection() {
            return direction;
        }

    public void setDirection(int direction) {
            this.direction = direction;
        }

    public Shot getShot() {
            return shot;
        }

    public void setShot(Shot shot) {
            this.shot = shot;
        }

    public void showShot() {
        if (shot != null && shot.isLive) {
            return;
        } else {
            Shot shot = new Shot(x, y, direction);
            setShot(shot);
            Thread t = new Thread(this.shot);
            t.start();
        }
    }

    public void moveLeft() {
        setDirection(3);
        setX(x - speed);
    }

    public void moveRight() {
        setDirection(1);
        setX(x + speed);
    }

    public void moveUp() {
        setDirection(0);
        setY(y - speed);
    }

    public void moveDown() {
        setDirection(2);
        setY(y + speed);
    }

    public void run() {
        while (isLive) {
            try {
                Thread.sleep(40);
            } catch (InterruptedException e) {
            }
            times--;
            if(times == 0) {
                r = generator.nextInt(5);
                times = 100;
            }
            if(times % 20 == 0) {
                showShot();
            }
            if(r == 0) {
                moveUp();
                if(y <= 0) {
                    r = 2;
                }
            } else if (r == 1) {
                moveRight();
                if(x >= 700) {
                    r = 3;
                }
            } else if (r == 2) {
                moveDown();
                if(y >= 500) {
                    r = 0;
                }
            } else if (r == 3) {
                moveLeft();
                if(x <= 0) {
                    r = 1;
                }
            }
        }
    }
}